﻿using CloudDcPc.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CloudDcPc.FuckDlg
{
    /// <summary>
    /// UserEditDlg.xaml 的交互逻辑
    /// </summary>
    public partial class UserEditDlg : Window
    {
        User user = null;
        public UserEditDlg(User user)
        {
            InitializeComponent();
            this.user = user;
            main_grid.DataContext = user;
            if (App.user.Type != 4)   //不是总经理
            {
                tb_zjl.Visibility = Visibility.Collapsed;
            }
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            if (uname.Text == "")
            {
                MessageBox.Show("用户名字不能为空！");
                return;
            }
            BindingExpression bind = uname.GetBindingExpression(TextBox.TextProperty);
            bind.UpdateSource();
            bind = dep.GetBindingExpression(TextBox.TextProperty);
            bind.UpdateSource();
            bind = type.GetBindingExpression(ComboBox.TextProperty);
            bind.UpdateSource();
            if (App.UserBean.UpdateUser(user))
            {


                MessageBox.Show("更新成功！");
                this.Close();
            }
        }

        /// <summary>
        /// 重置密码按钮
        /// </summary>
        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            string messageBoxText = "确认重置该用户密码吗？";
            string caption = "重置密码";
            MessageBoxButton button = MessageBoxButton.YesNo;
            MessageBoxImage icon = MessageBoxImage.Warning;
            MessageBoxResult result = MessageBox.Show(messageBoxText, caption, button, icon);
            if (result == MessageBoxResult.Yes)
            {
                if (App.UserBean.ResetPwdById(user.Id))
                {
                    MessageBox.Show("重置成功！新密码和工号相同！");
                }
                else
                {
                    MessageBox.Show("重置失败！");
                }
            }
        }

        /// <summary>
        /// 关闭按钮
        /// </summary>
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            string messageBoxText = "确认删除该员工吗？";
            string caption = "删除员工";
            MessageBoxButton button = MessageBoxButton.YesNo;
            MessageBoxImage icon = MessageBoxImage.Warning;
            MessageBoxResult result = MessageBox.Show(messageBoxText, caption, button, icon);
            if (result == MessageBoxResult.Yes)
            {
                if (App.UserBean.DelUserById(user.Id))
                {
                    MessageBox.Show("删除成功！");
                    this.DialogResult = true;
                    this.Close();
                }
                else
                {
                    MessageBox.Show("删除失败！");
                }
            }
        }
    }
}
